package com.bifrii.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.bifrii.transferObjects.Admin;

public class JDBCAdminDAO implements IAdminDAO {

	@Override
	public void insert(Admin a) {
Connection conn = ConnectionManager.getInstance().checkOut();
		
		String sql = "INSERT INTO Admin (oid, idUser) VALUES (?, ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, a.getOid());
            stmt.setInt(2, a.getIdUser());
            

            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }

	}

	@Override
	public Admin select(Integer idUser) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
        ResultSet result = null;
        Admin a = null;
        String sql = "SELECT * FROM Admin WHERE (idUser = ?) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, idUser);
            result = stmt.executeQuery();

            result.next();
            a = new Admin();
            a.setIdUser(result.getInt("idUser"));
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return a;
	}

}
